package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CustomerSetGroupService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.customersetgroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CustomerSetGroupDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CustomerSetGroupMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 客户组明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CustomerSetGroupServiceImpl implements CustomerSetGroupService {

    @Resource
    private CustomerSetGroupMapper customerSetGroupMapper;

    @Override
    public Long createCustomerSetGroup(CustomerSetGroupSaveReqVO createReqVO) {
        // 插入
        CustomerSetGroupDO customerSetGroup = BeanUtils.toBean(createReqVO, CustomerSetGroupDO.class);
        customerSetGroupMapper.insert(customerSetGroup);
        // 返回
        return customerSetGroup.getId();
    }

    @Override
    public void updateCustomerSetGroup(CustomerSetGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerSetGroupExists(updateReqVO.getId());
        // 更新
        CustomerSetGroupDO updateObj = BeanUtils.toBean(updateReqVO, CustomerSetGroupDO.class);
        customerSetGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerSetGroup(Long id) {
        // 校验存在
        validateCustomerSetGroupExists(id);
        // 删除
        customerSetGroupMapper.deleteById(id);
    }

    private void validateCustomerSetGroupExists(Long id) {
        if (customerSetGroupMapper.selectById(id) == null) {
            throw exception(CUSTOMER_SET_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public CustomerSetGroupDO getCustomerSetGroup(Long id) {
        return customerSetGroupMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerSetGroupDO> getCustomerSetGroupPage(CustomerSetGroupPageReqVO pageReqVO) {
        return customerSetGroupMapper.selectPage(pageReqVO);
    }

}